﻿Imports System.IO

Public Class Juego
    Private _palabras As New List(Of Palabra)
    Public _palabraActual As New Palabra
    Private HanSalido As New List(Of Integer)
    Dim random As New Random
    Private Property errores As New Integer
    Private Property caracteres As New Integer


    Public Function almacenar(ByVal enun As String, ByVal descrip As String) As String
        If enun Is Nothing OrElse enun = "" Then
            Return "Enunciado vacio"
        End If

        For i = 0 To _palabras.Count - 1
            If _palabras(i).enunciado = enun Then
                Return "Enunciado repetido"
            End If
        Next

        _palabras.Add(New Palabra(enun, descrip))

        Return "Añadida"
    End Function

    Public Function borrar(ByVal enun As String) As String
        For i As Integer = 0 To _palabras.Count - 1
            If _palabras(i).enunciado.ToLower = enun.ToLower Then
                _palabras.RemoveAt(i)
                Return "Borrada"
            End If
        Next
        Return "Error, no existe esa palabra"
    End Function

    Public Sub borrarTodas()
        _palabras.Clear()
    End Sub


    Public Function palabraAzar() As Palabra
        Dim azar As Integer
        If HanSalido.Count >= _palabras.Count Then Return Nothing
        Do
            azar = random.Next(_palabras.Count)

        Loop Until Not HanSalido.Contains(azar)
        HanSalido.Add(azar)
        _palabraActual = _palabras(azar)
        Return _palabras(azar)
    End Function

    Public Function PosicionesLetra(ByVal letra As Char) As List(Of Integer)
        Dim posiciones As New List(Of Integer)

        If Not _palabraActual.enunciado.Contains(letra) Then
            errores += 1
            Return New List(Of Integer)
        End If
        For i = 0 To _palabraActual.enunciado.Length - 1
            If _palabraActual.enunciado.Chars(i) = letra Then
                caracteres += 1
                posiciones.Add(i)

            End If


        Next
        Return posiciones

    End Function

    'Public ReadOnly Property Ahorcado As String
    '    Get
    '        If errores = 5 Then
    '            Return "Ahorcado"
    '        End If
    '        Return ""
    '    End Get
    'End Property
    Public ReadOnly Property Ahorcado As Boolean
        Get
            Return errores = 5
        End Get
    End Property
    Public ReadOnly Property Adivinado As Boolean
        Get
            Return _palabraActual.enunciado.Count = caracteres
        End Get
    End Property
    Public Sub Reiniciar()
        HanSalido.Clear()
    End Sub

    Public Sub New()
        'Dim fich As System.IO.StreamReader
        Dim fich As StreamReader 'para leer del fichero y llevarlo a memoria

        If File.Exists("palabras.txt") Then
            Try
                fich = New StreamReader("palabras.txt")
                Do Until fich.EndOfStream
                    Dim datos() As String
                    Dim linea As String

                    linea = fich.ReadLine
                    datos = linea.Split("*")

                    _palabras.Add(New Palabra(datos(0), datos(1)))

                Loop

            Catch ex As Exception
                _palabras.Clear()
            Finally
                fich.Close()
            End Try
        End If

    End Sub

    Public Function Guardar() As String
        Dim fich As StreamWriter
        fich = New StreamWriter("lista.txt")
        Try
            For i = 0 To _palabras.Count - 1
                fich.WriteLine(_palabras(i).enunciado & "*" & _palabras(i).descripcion.ToString)
            Next

        Catch ex As Exception
            Return ex.Message
        End Try

        fich.Close()

    End Function
End Class
